<?php
	require_once $ubi['SERVER']['SERVER_ROOT'] . '/dao/daoQuestion.php';	 
	require_once $ubi['SERVER']['SERVER_ROOT'] . '/dao/daoProvider.php';
	require_once $ubi['SERVER']['SERVER_ROOT'] . '/dao/daoItem.php';
	
	require_once $ubi['SERVER']['SERVER_ROOT'] . '/model/question.php';
	require_once $ubi['SERVER']['SERVER_ROOT'] . '/model/provider.php';
	require_once $ubi['SERVER']['SERVER_ROOT'] . '/model/item.php';
	 
	class QuestionBus {
		private $_questionDao;
		private $_providerDao;
		private $_itemDao;	
			
		public function __construct() {
			$this->_questionDao = new QuestionDao();
			$this->_providerDao = new ProviderDao();
			$this->_itemDao = new ItemDao();		
		}
		
		public function getQuestionProviderList($cat) {
			$listQuestion = $this->_questionDao->getQuestionProviderList($cat);
			return $listQuestion;
		}
		
		public function getQuestionItemList($arr) {
			$listQuestion = $this->_questionDao->getQuestionItemList($arr);
			return $listQuestion;
		}
		
		public function getQuestionProviderListXML($cat) {
			$xml = '<Questions>';
			
			$listQuestion = $this->getQuestionProviderList($cat);
			
			for ($i = 0; $i < count($listQuestion); $i++) {
				$question = $listQuestion[$i];
				$xml .= '<Question id="' . $question->getQuestionID() . '" content="' 
						. $question->getQuestion() . '">';
				$xml .= $question->getAnswers() . '</Question>';
			}
				
			$xml .= '</Questions>';
			
			return $xml;
		}

		public function getQuestionItemListXML($arr) {
			$xml = '<Questions>';
			
			$listQuestion = $this->getQuestionItemList($arr);
			for ($i = 0; $i < count($listQuestion); $i++) {
				$question = $listQuestion[$i];
				$xml .= '<Question id="' . $question->getQuestionID() . '" content="' 
						. $question->getQuestion() . '">';
				$xml .= $question->getAnswers() . '</Question>';
			}
				
			$xml .= '</Questions>';
			
			return $xml;
		}
		
		private function getAnswerString($answerList, $selectedID) {
			$doc = new DOMDocument();
			$doc->loadXML($answerList);
			$arrAnswer = $doc->getElementsByTagName("answer");
			
			for ($i = 0; $i < $arrAnswer->length; $i++) {
				$id = $arrAnswer->item($i)->getAttribute('id');
				if ($id == $selectedID) {
					$answer = $arrAnswer->item($i)->getAttribute('content');
					
					return $answer;
				}					
			}
		}
		
		public function getQuestionAnswerListFromProvider($providerID, &$arrQuestion, &$arrAnswer) {			
			$provider = $this->_providerDao->getProvider($providerID);
			$answerList = $provider->getAnswerList();
			if ($answerList != null) {
				$arrTemp = explode(' ', trim($answerList));
				
				foreach ($arrTemp as $temp) {
					$arr = explode('-', $temp);
					$question = $this->_questionDao->getQuestionProvider($arr[0]);
					if (isset($question)) {
						$answer = $this->getAnswerString($question->getAnswers(), $arr[1]);
						$arrQuestion[] = $question->getQuestion();
						$arrAnswer[] = $answer;
					}			
				}
			}
		}

		public function getQuestionAnswerListFromItem($itemID, &$arrQuestion, &$arrAnswer) {			
			$item = $this->_itemDao->getItem($itemID);
			$answerList = $item->getAnswerList();
			if ($answerList != null) {
				$arrTemp = split(' ', trim($answerList));
				
				foreach ($arrTemp as $temp) {
					$arr = split('-', $temp);		
					$question = $this->_questionDao->getQuestionItem($arr[0]);
					if (isset($question)) {
						$answer = $this->getAnswerString($question->getAnswers(), $arr[1]);
						$arrQuestion[] = $question->getQuestion();
						$arrAnswer[] = $answer;
					}		
				}
			}
		}
	} 
?>